<ui:composition template="/WEB-INF/templates/showcase.xhtml"
	xmlns="http://www.w3.org/1999/xhtml"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:o="http://omnifaces.org/ui"
	xmlns:of="http://omnifaces.org/functions"
>
	<ui:define name="description">
		<p>
			Collection of EL functions for data conversion: <code>of:iterableToList()</code> (with alternative <code>of:iterableToModel</code>), 
			<code>of:setToList()</code>, <code>of:mapToList()</code>, <code>of:joinArray()</code>, <code>of:joinCollection()</code>, 
			<code>of:joinMap()</code> and <code>of:toJson()</code>.
		</p>
		<p>
			Regarding the <code>of:xxxToList()</code> functions; <code>&lt;ui:repeat&gt;</code> (and <code>&lt;h:dataTable&gt;</code>) 
			doesn't support <code>Iterable</code> (such as <code>Set</code>) and <code>Map</code> directly, so those functions may be 
			handy for them.
			If however EL 2.2 is used, then e.g. <code>\#{bean.set.toArray()}</code> (for <code>Collection</code> types) and 
			<code>\#{bean.map.entrySet().toArray()}</code> could be used instead.
			But if EL 2.2 is not supported or for a general <code>Iterable</code> the provided EL functions can be used.
		</p>
		<p>
			The <code>of:joinXxx()</code> functions basically joins the elements of the array, collection or map to a 
			string using the given separator.
		</p>
		<p>
			The <code>of:toJson()</code> function encodes any object to a string in JSON format according the rules of
			<a href="#{_apiURL}/org/omnifaces/util/Json.html#encode(java.lang.Object)">Json#encode()</a>.
		</p>
		<p>
			Note that since JSF 2.2 it should be possible to use <code>\#{bean.set}</code> directly in <code>&lt;h:dataTable&gt;</code>, but not
			in <code>&lt;ui:repeat&gt;</code>.
			The <code>of:setToList()</code> thus remains useful for JSF 2.0 and 2.1 in all cases, but is not needed for 
			<code>&lt;h:dataTable&gt;</code> in JSF 2.2.
		</p>
		<ui:include src="/WEB-INF/includes/utils/feature-request.xhtml" />
	</ui:define>		
	
	<ui:define name="demo">
		<h3>Using <code>of:xxxToList/Model()</code> functions</h3>
		<p>
			Convert <code>Set&lt;E&gt;</code> to <code>List&lt;E&gt;</code> using <code>\#{of:setToList()}</code>:
			<strong>
				<ui:repeat value="#{of:setToList(convertersBean.set)}" var="item" varStatus="loop">
					#{item}#{!loop.last ? ', ' : ''}
				</ui:repeat>
			</strong>
		</p>
		<p>
			Convert <code>Iterable&lt;E&gt;</code> to <code>List&lt;E&gt;</code> using <code>\#{of:iterableToList()}</code>:
			<strong>
				<ui:repeat value="#{of:iterableToList(convertersBean.iterable)}" var="item" varStatus="loop">
					#{item}#{!loop.last ? ', ' : ''}
				</ui:repeat>
			</strong>
		</p>
		<p>
			(Alternative) Convert <code>Iterable&lt;E&gt;</code> to <code>DataModel&lt;E&gt;</code> using <code>\#{of:iterableToModel()}</code>:
			<strong>
				<ui:repeat value="#{of:iterableToModel(convertersBean.iterable)}" var="item" varStatus="loop">
					#{item}#{!loop.last ? ', ' : ''}
				</ui:repeat>
			</strong>
		</p>
		<p>
			(Alternative) Convert <code>Set&lt;E&gt;</code> to <code>DataModel&lt;E&gt;</code> using <code>\#{of:iterableToModel()}</code>:
			<strong>
				<ui:repeat value="#{of:iterableToModel(convertersBean.set)}" var="item" varStatus="loop">
					#{item}#{!loop.last ? ', ' : ''}
				</ui:repeat>
			</strong>
		</p>
		<p>
			Convert <code>Map&lt;K, V&gt;</code> to <code>List&lt;Entry&lt;K, V&gt;&gt;</code> using <code>\#{of:mapToList()}</code>:
			<strong>
				<ui:repeat value="#{of:mapToList(convertersBean.map)}" var="entry" varStatus="loop">
					#{entry.key}=#{entry.value}#{!loop.last ? ', ' : ''}
				</ui:repeat>
			</strong>
		</p>
		
		<hr />

		<h3>Just to show EL 2.2 ability to invoke non-getter methods, so that <code>of:xxxToList()</code> are not necessary</h3>
		<p>
			Use <code>\#{bean.set.toArray()}</code> directly:
			<strong>
				<ui:repeat value="#{convertersBean.set.toArray()}" var="item" varStatus="loop">
					#{item}#{!loop.last ? ', ' : ''}
				</ui:repeat>
			</strong>
		</p>
		<p>
			Use <code>\#{bean.map.entrySet().toArray()}</code> directly:
			<strong>
				<ui:repeat value="#{convertersBean.map.entrySet().toArray()}" var="entry" varStatus="loop">
					#{entry.key}=#{entry.value}#{!loop.last ? ', ' : ''}
				</ui:repeat>
			</strong>
		</p>
		
		<hr />
		
		<h3>Using <code>of:joinXxx()</code> functions</h3>
		<p>
			Join array to string using <code>\#{of:joinArray()}</code>: 
			<strong>#{of:joinArray(convertersBean.array, ', ')}</strong>
		</p>
		<p>
			Join collection to string using <code>\#{of:joinCollection()}</code>:
			<strong>#{of:joinCollection(convertersBean.set, ', ')}</strong>
		</p>
		<p>
			Join map to string using <code>\#{of:joinMap()}</code>: 
			<strong>#{of:joinMap(convertersBean.map, '=', ', ')}</strong>
		</p>
		
		<hr />
		
		<h3>Encode any object to JSON</h3>
		<pre class="prettyprint"><code class="lang-javascript">
var array = #{of:toJson(convertersBean.array)};
var collection = #{of:toJson(convertersBean.set)};
var map = #{of:toJson(convertersBean.map)};
var exampleEntities = #{of:toJson(selectItemsBean.exampleEntities)};
var serverDate = new Date(#{of:toJson(now)});
		</code></pre>
	</ui:define>
</ui:composition>